package cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.service;

import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.entity.AbsAchievement;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.entity.AchievementType;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.entity.Employee;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.entity.Paper;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.mapper.AbsAchievementMapper;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.mapper.PaperMapper;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.util.SnowflakeIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;

@Service
public class PaperService {
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;
    @Autowired
    private AbsAchievementMapper absAchievementMapper;

    public PageInfo<Paper> get(int pageNum, int pageSize, Paper paper){
        PageHelper.startPage(pageNum,pageSize);
        List<Paper> papersList  = paperMapper.get(paper);
        return PageInfo.of(papersList);
    }
    @Transactional
    public String save(Paper paper){

        Long id = snowflakeIdGenerator.generateId();
        AchievementType achievementType = new AchievementType(3l,"论文","THESIS");
        paper.setAchievementType(achievementType);
        absAchievementMapper.addAchievement(id,achievementType);
        paper.setId(id);
        if(paperMapper.save(paper)){
            return "保存成功";
        }
        return "保存失败";
    }
    public String update(Paper paper){
        if(paperMapper.update(paper)){
            return "修改成功";
        }
        System.out.println("修改失败");
        return "修改失败";
    }
    public String delete(int id){
        if(paperMapper.delete(id)){
            return "删除成功";
        }
        return "删除失败";
    }
}
